#include "myinc.h"
#include "CAFSA.h"


int main(int argc, char *argv[])
{

    VideoCapture cap(argv[1]);
    if(!cap.isOpened()){
        cout<< "fail to open"<<endl;
        return -1;
    }
    int frameToStart = 200;
    cap.set( CV_CAP_PROP_POS_FRAMES,frameToStart);
    //int frameToStop = 100;

    Mat image;// = imread("../.jpg");
    cap.read(image);
    resize(image,image,Size(640,360));
    imshow("Origin", image);
	//imwrite("Green.JPG", image);
	waitKey(0);

    int af_num = 8;
    int ite_num =20;
    float x_min = 0.1;
    float y_min = 0.1;
    float z_min = 0.1;
    float x_max = 150.0;
    float y_max = 150.0;
    float z_max = 150.0;
    int step = 60;
    float visual = 40.0;
    int try_num = 10;
    float delta = 0.1;
    float delta_s = 0.1;
    float max_blur, max_contrast, max_balance;
    float min_blur, min_contrast, min_balance;
    float wei_blur = 1;
    float wei_contrast = 1;
    float wei_balance = 1;
    vector<float> final_contrast;
    //float b_cont, g_cont, r_cont, max_cont;
    Mat image_tmp, image_tmp2, image_tmp3;

    CFilt filter(image.rows, image.cols);
    CValue valuer(image.rows, image.cols);

    image_tmp = filter.wiener(image.clone(), x_min, y_min, z_max);
    min_blur = valuer.get_blur_value(image_tmp);
    //max_balance = valuer.get_balance_value(image_tmp.clone());

    image_tmp = filter.wiener(image.clone(), x_max, y_max, z_max);
    max_blur = valuer.get_blur_value(image_tmp);

    image_tmp = filter.wiener(image.clone(), x_max, y_max, z_min);
    min_contrast = valuer.get_contrast_value(image_tmp);

    image_tmp = filter.wiener(image.clone(), x_min, y_min, z_max);
    max_contrast = valuer.get_contrast_value(image_tmp);
    min_balance = 0;
    max_balance = 510;

    cout<<max_blur<<","<<max_contrast<<","<<max_balance<<endl;
    cout<<min_blur<<","<<min_contrast<<","<<min_balance<<endl;

    /*image_tmp = filter.wiener(image.clone(), 150, 150, 150);*/
    //imshow("test",image_tmp);
    //valuer.get_blur_value(image_tmp);
    //float a = valuer.get_blur_value(image_tmp);
    //float b = valuer.get_contrast_value(image_tmp);
    //float c = valuer.get_balance_value(image_tmp);
    //cout<<a<<endl<<b<<endl<<c<<endl<<wei_blur*a+wei_contrast*b-wei_balance*c<<endl;
    /*waitKey(0);*/

    clock_t s, t;
    s = clock();
    srand((int)time(0));

    CAFSA af_go(af_num, ite_num, image.rows, image.cols, image, x_min, x_max, y_min, y_max, z_min, z_max, step, visual, try_num, delta, delta_s, max_blur, max_contrast, max_balance, min_blur, min_contrast, min_balance, wei_blur, wei_contrast, wei_balance);
    af_go.evaluate();
    t = clock();
    double du = (double)(t-s);
    cout<<du/CLOCKS_PER_SEC <<endl;

    //filter.get_wf(af_go.x_best, af_go.y_best);
    //image = filter.wiener(image.clone(), af_go.z_best);
 /*   final_contrast = valuer.get_contrast_value(image_tmp.clone(), 1);*/
    //b_cont = final_contrast.back();
    //max_cont = b_cont;
    //final_contrast.pop_back();
    //g_cont = final_contrast.back();
    //if(g_cont > max_cont) max_cont = g_cont;
    //final_contrast.pop_back();
    //r_cont = final_contrast.back();
    //if(r_cont > max_cont) max_cont = r_cont;
    //final_contrast.pop_back();
    /*cout<<b_cont/ max_cont<<","<<g_cont/ max_cont<<","<<r_cont/ max_cont<<endl;*/
    //imshow("fliter", image);
    //filter.light_comp(image.clone());
    filter.get_wf(af_go.x_best, af_go.y_best);
    image_tmp = filter.wiener(image.clone(), af_go.x_best, af_go.y_best, af_go.z_best);
    imshow("Filt", image_tmp);
    image_tmp = filter.clahe(image.clone());
    imshow("CLAHE", image_tmp);
    image_tmp = filter.wiener(image.clone(), af_go.z_best);
    imshow("Final", image_tmp);
    //filter.light_comp(image);
    waitKey(0);
    while(1){
        cap.read(image);
        resize(image,image,Size(640,360));
    	s = clock();
        image_tmp = filter.wiener(image.clone(), af_go.z_best);
	    t = clock();
    	double du = (double)(t-s);
    	cout<<du/CLOCKS_PER_SEC <<endl;
        image_tmp2 = filter.clahe(image.clone());
        image_tmp3 = filter.wiener(image.clone(),af_go.x_best, af_go.y_best, af_go.z_best);
    //for(int i=0; i<10; ++i){
    //float a = rand()/double(RAND_MAX);
    //cout<< a <<endl;}
        imshow("Origin", image);
        imshow("Filt", image_tmp3);
        imshow("CLAHE", image_tmp2);
        imshow("Final", image_tmp);
        char key = waitKey(1);
        if(key == 27)
            break;
		else if(key == ' ')
			waitKey(0);
    }
    return 0;
}
